<?php

if(stristr($_SERVER['PHP_SELF'],".inc.php")) { 
	header("Location:index.php"); 
	exit; 
}

if($_REQUEST['preview'] || ($_REQUEST['op'] == "p")) {
	$_hideForm = TRUE;
	$_hidePosts = TRUE; 
	if($_GET['post']) { //if the post was passed via URL
		$REQUEST['post'] = urldecode($_GET['post']);  
	} else { 
		$REQUEST['post'] = $_REQUEST['post'];
	}
	
/* ########################## preview section ########################## */
	
	if(!$_POST['pubdate']) { $_POST['pubdate']= date("m-d-Y H:i:s"); }
	$pubdate = parse_pubdate($_POST['pubdate'],"u");
	$teaser = str_replace("<?","&lt;?",$_POST['teaser']);
	if(!$CONFIG['allow_js']) { 
		$teaser = str_replace("<script","&lt;script",$teaser);
		$teaser = str_replace("</script","&lt;/script",$teaser);
	}
	$Xpost = str_replace("<?","&lt;?",$REQUEST['post']);
	if(!$CONFIG['allow_js']) { 
		$Xpost = str_replace("<script","&lt;script",$Xpost);
		$Xpost = str_replace("</script","&lt;/script",$Xpost);
	}
	
	
	//str_replace("\r\n","<br />",trim(stripslashes($Xpost))); 
	$preview = array(); 
	$topic = get_topic($_POST['topic']); 
	$preview['title'] = stripslashes($_REQUEST['title']);
	$preview['time'] = date($CONFIG['datefrmt'],$pubdate);
	$preview['tImg'] = $topic['img'];
	$preview['tName'] = addslashes($topic['name']);
	$preview['tAlign'] = "left";
	$preview['teaser'] = nl2br(stripslashes($teaser));
	$preview['teaser'] 	= preg_replace( "/\[\[(\S+)\|(.+)\]\]/U", "<a href=\"\\1\">\\2</a>", $preview['teaser']);	
	$preview['pubdate'] = $_POST['pubdate'];
	$preview['post'] = nl2br(stripslashes($_POST['post']));
	$preview['post'] 	= preg_replace( "/\[\[(\S+)\|(.+)\]\]/U", "<a href=\"\\1\">\\2</a>", $preview['post']);
	$preview['permalink_title'] = permalink_title($preview['title']); 
	$preview['publicPath'] = $publicPath; 
	$preview['lock'] = ""; 

	$tags = explode(",",stripslashes($_POST['tags'])); 
	if(is_array($tags)) { 	
		$preview['tags'] = "<div class=\"tagspan\">Tags: ";
		foreach($tags as $k=>$v) { 
			$tag = cat_to_tag($v); 
			$preview['tags'] .= "<a href=\"#\">".trim($tag)."</a>, ";
		}
		$preview['tags'] = substr(trim($preview['tags']),0,-1); 
		$preview['tags'] .= "</div>";
	}
	
	#################### TEMPLATE #########################################
	$tmpl = $tmpldir."read.tmpl";
	if(phpversion() < 4.3) { 
		$html = implode ("", (file($tmpl)));
	} else { 
		$html = file_get_contents($tmpl);  
	}
	//this next bit generates the comment from the HTML template 
	foreach ($preview as $key => $value) { 
		$mykey = '{' . $key . '}';
		$html = str_replace($mykey, $value, $html);
	}    
	echo $html;
	#################### END TEMPLATE #####################################


?>
	<a name="add">&nbsp;</a>
	<form action="<?php echo $self; ?>" method="post" name="myform">
	<div id="add_item">
		<div id="add_item_header"><?php echo $Lang['preview'] . " " . $Lang['post']; ?></div>
	
	<div id="add_item_title">
		<input class="item_title" type="text" value="<?=htmlspecialchars(stripslashes($_REQUEST['title']));?>" name="title" maxlength="100" />
	</div>

	<fieldset id="add_item_teaser">
		<legend><?=$Lang['teaser'];?></legend>
		<textarea name="teaser" id="teaser"><?= htmlspecialchars(stripslashes($_REQUEST['teaser'])); ?></textarea>
		<br />
		<input type="button" class="button" value="Bold" name="b" onclick="boldify2('teaser')">&nbsp;
		<input type="button" class="button" value="Italic" name="i" onclick="italify2('teaser')">&nbsp;
		<input type="button" class="button" value="Underline" name="u" onclick="underlinify2('teaser')">&nbsp;
		<input type="button" class="button" value="Insert Link" name="href" onclick="linkify2('teaser')">&nbsp;
		<input type="button" class="button" value="Insert Image" name="img" onclick="imagify2('teaser')">
	</fieldset>
	
<?php if(strlen($_REQUEST['post']) > 2) { $aibc="none"; $aib="block"; }  else { $aibc="block";$aib="none"; } ?>	
	<div id="add_item_body_collapsed" style="display:<?=$aibc;?>;">
		<button onclick="javascript:toggle_body()" type="button" class="button">Show Body</button>
	</div>
	<fieldset id="add_item_body" style="display:<?=$aib;?>">
		<legend><?=$Lang['post'];?></legend>
		<textarea name="post" id="post" cols="50" rows="10"><?= htmlspecialchars(stripslashes($_REQUEST['post'])); ?></textarea>
		<br/><input type="button" class="button" value="Bold" name="b" onclick="boldify2('post')">&nbsp;
		<input type="button" class="button" value="Italic" name="i" onclick="italify2('post')">&nbsp;
		<input type="button" class="button" value="Underline" name="u" onclick="underlinify2('post')">&nbsp;
		<input type="button" class="button" value="Insert Link" name="href" onclick="linkify2('post')">&nbsp;
		<input type="button" class="button" value="Insert Image" name="img" onclick="imagify2('post')"><br />
		<br />
		<input type="button" onclick="javascript:toggle_body()" class="button" value="Hide Body" />
	</fieldset>	

	<fieldset id="metadata">
		<legend><?=ucwords($Lang['metadata']);?></legend>
		<?php echo ucwords($Lang['topic']); ?><br />
		<select name="topic">
<?php
	foreach($topics as $k=>$v) { 
		echo "\t\t<option value=\"".$v['topicid']."\"";
		if($v['topicid']==$_POST['topic']) { echo " selected=\"selected\""; }
		echo ">".$v['name']."</option>\n";
	}
?>	
		</select>
	<br /><br />
	
	<?php echo ucwords($Lang['tags']); ?>: <input id="tags" name="tags" type="text" value="<?=$_POST['tags'];?>" size="40">
		<br /><small>Tags should be separated by a comma</small>
	</fieldset>
	
	<fieldset id="add_item_pubdate">
		<legend><?php echo ucwords($Lang['pubdate']); ?></legend>
		<input id="pubdate" name="pubdate" type="text" value="<?php echo date("Y-m-d G:i:s"); ?>" size="25">
		<a href="javascript:NewCal('pubdate','YYYYMMDD',true)">
		<img src="./inc/datetime/cal.gif" width="16" height="16" border="0" alt="Pick a date" /></a>
	</fieldset>
		
	<fieldset id="add_item_public">
		<legend><?php echo ucwords($Lang['public']); ?></legend>
		<input type="radio" name="public" value="1" checked="checked"><?php echo $Lang['unhideThisPost']; ?>
		<input type="radio" name="public" value="0"><?php echo $Lang['hideThisPost']; ?>
		</select>
	</fieldset>

	<div id="add_item_buttons">
		<input type="submit" name="preview" class="button" value="<?php echo $Lang['preview']. " " . $Lang['post']; ?>" />
		&nbsp;&nbsp;
		<input type="submit" name="add" class="button" value="<?php echo $Lang['add'] . " " . $Lang['post']; ?>" />
	</div>

	</div>
	</form> 
	
<?php
}
?>